#include "CKeyboard.h"

cKeyboard::cKeyboard(DWORD keyboardCoopFlags, IDirectInput8 *pDInput, HWND hWnd)
{
	//Zero out the keyboard state
	ZeroMemory(m_keyboardState, sizeof(m_keyboardState));

	//Create the keyboard device
	pDInput->CreateDevice(GUID_SysKeyboard, &m_pKeyboard, 0);

	//Set the data format of the device
	m_pKeyboard->SetDataFormat(&c_dfDIKeyboard);

	//Set the cooperative levels
	m_pKeyboard->SetCooperativeLevel(hWnd, keyboardCoopFlags);

	//Finally, acquire the device
	m_pKeyboard->Acquire();
}

cKeyboard::~cKeyboard()
{
	if(m_pKeyboard)
	{
		m_pKeyboard->Release();
		m_pKeyboard = NULL;
	}
}

bool cKeyboard::isKeyPressed(char key)
{
	return m_keyboardState[key] & 0x80;
}

void cKeyboard::Poll()
{
	//Poll Keyboard
	HRESULT hr = m_pKeyboard->GetDeviceState(sizeof(m_keyboardState), 
												(LPVOID)&m_keyboardState);
	
	//
	if(FAILED(hr))
	{
		ZeroMemory(m_keyboardState, sizeof(m_keyboardState));

		hr = m_pKeyboard->Acquire();
	}
}